package models;
 
import java.math.BigDecimal;
import java.util.*;

import javax.persistence.*;
 
import play.data.validation.Required;
import play.db.jpa.*;
 
@Entity
public class Transaction extends Model {

	@Required
	@ManyToOne
	public Customer customer;
 
	
	@ManyToOne
	public Position position;

	public Date execute_date;
	
	
	public BigDecimal shares;
	
	@Required
	public String transaction_type; //TODO: we may want to make this an enum
	
	
	public BigDecimal amount; 
	
 
	public Transaction(Customer customer, Position position, 
			BigDecimal shares, String transaction_type, BigDecimal amount) {
		this.customer = customer;
		this.position = position;
	//	this.execute_date = null;//execute_date;
		this.shares = shares;
		this.transaction_type = transaction_type;
		this.amount = amount;
		
	}
	
	// XG this is for deposit checks
	public Transaction(Customer customer, String transaction_type, BigDecimal amount){
		this.customer = customer;
	//	this.execute_date = execute_date;
		this.transaction_type = transaction_type;
		this.amount = amount;
	}
	
	public String toString() {
		if(this.position!=null){//XG in order to put "Deposit" record into Transaction table and display it successfully
		return this.position.toString() + " - " + this.transaction_type.toString(); //+ truncate(this.execute_date.toString(), 10);
		}
		return this.amount+" - "+this.customer.username+" - "+"NULL" + " - " + this.transaction_type.toString();//+ truncate(this.execute_date.toString(), 10);
	}
	
	private static String truncate(String value, int length)
	{
	  if (value != null && value.length() > length)
	    value = value.substring(0, length);
	  return value;
	}
}